package com.example.weatherview.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Paint.Align
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.graphics.withTranslation
import kotlin.math.roundToInt

class RainView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    private val mScalePaint = Paint().apply {
        textSize = sp2Px(context,12F).toFloat()
        //color = Color.parseColor("#FFFFFF")
        color = Color.BLACK
    }


    private val mAxisPaint = Paint().apply {
        strokeWidth = dp2Px(context,1F).toFloat()
        color = Color.BLACK
    }

    private val mBarPaint = Paint().apply {
        strokeWidth = dp2Px(context,12f).toFloat()
        color = Color.parseColor("#FBBB21")
        style = Paint.Style.FILL
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val w = MeasureSpec.getSize(widthMeasureSpec)
        val h = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(w,h)
    }

    private fun Canvas.drawAxis(){
        //35  20
       val marginLeft = dp2Px(context,35f).toFloat()
       val marginBottom = dp2Px(context,20f).toFloat()


        //X
        drawLine(marginLeft,-marginBottom,width.toFloat(),-marginBottom,mAxisPaint)
        //Y
        drawLine(marginLeft,-marginBottom,marginLeft,-height.toFloat(),mAxisPaint)
    }


    private val mScaleY = arrayListOf(0,50,100)


    private var mMaxNum = 5


    private val mRainData = arrayListOf(
        RainData(100,"16:00"),
        RainData(80,"17:00"),
        RainData(50,"18:00"),
        RainData(40,"19:00"),
        RainData(20,"20:00"),
    )


    private fun Canvas.drawAxisScale() {
        if (mRainData.isEmpty()) return
        val marginLeft = dp2Px(context,35f).toFloat()
        //Y
        val margin = dp2Px(context, 20 + 8f).toFloat()
        val realHeight = height-margin
        val intervalY = realHeight / 2f
        val textH_Halt = dp2Px(context,10f).toFloat()
        mScaleY.forEachIndexed { index, i ->
            mScalePaint.textAlign = Align.RIGHT
            val offsetY = if (i>0) textH_Halt else 0f
            val offsetX = dp2Px(context,4f).toFloat()
            drawText("$i%",marginLeft-offsetX,-margin-(index*intervalY)+offsetY,mScalePaint)
        }

        //X
        val offsetBottom = dp2Px(context,5f).toFloat()
        val marginY = dp2Px(context, 20 + 8f).toFloat()
        val realWidth = width-marginLeft
        val dateStr = mRainData[0].date
        val measureText = mScalePaint.measureText(dateStr)
        val totalIntervalX = realWidth - (measureText*mMaxNum)
        val intervalX = totalIntervalX /(mMaxNum+ 1)
        Log.d("drawAxisScale", "$width  $marginLeft  $realWidth   $measureText  $intervalX")

        /**
         * 1
         * 2 +t
         * 3+2t
         * 4+3t
         * 5+4T
         */


        /**
         * 1+0.5t
         * 2+1.5t
         * 3+2.5t
         * 4+3.5t
         * 5+4.5t
         *
         */

        /**
         *  100 -height
         *
         *
         * 0 -marginY
         */

        mRainData.forEachIndexed { index, rainData ->
            mScalePaint.textAlign = Align.LEFT
            val startX = ((index + 1) * intervalX) + (index * measureText)+marginLeft
            drawText(rainData.date,startX,-offsetBottom,mScalePaint)

           val startLineX =  ((index + 1) * intervalX) +(index * measureText)+(measureText/2F) + marginLeft


            val lineHeight = rainData.value*(realHeight/100f)
            drawLine(startLineX,-marginY,startLineX,-lineHeight-marginY,mBarPaint)
        }
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.withTranslation(0f,height.toFloat()){
            drawAxis()
            drawAxisScale()
        }
    }



    private fun dp2Px(context: Context, dp: Float): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density).roundToInt()
    }

    private fun sp2Px(context: Context, sp: Float): Int {
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        return Math.round(sp * scaledDensity)
    }
}

data class RainData(val value: Int,val date:String)

